<%@ include file="../import_page.jsp"%>
<%@ page import="java.util.List"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page
	import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.student)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	//intput parament
	int acId = -1;
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	try {
		if(isEmptyString(acIdStr))
			throw new Exception("No parament input");
		acId = Integer.parseInt(acIdStr);
		if(acId < 1)
			throw new Exception("Incorrect assignment ID");
	} catch (Exception err) {
		//if fail to get parament, go back
		response.sendRedirect(URL_SELECT_ASSIGNMENT);
		return;
	}
	
	Users loginUser = (Users)session.getAttribute(SESSION_LOGIN_USER);
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;
	String filename = null;
	long fileSize = -1; 
	
	//get assignment first
	AssignmentCollector ac = null;
	try {
		AssignmentsControl assCon = new AssignmentsControl();
		ac = assCon.getAssignment(acId);
		assCon.closeDBConnection();
		
	} catch (Exception err) {
		errMsg = err.toString();
	}
	
	
	//Check if the assignment is expired, return to view assignment page
	if(ac.isExpired() || !isEmptyString(errMsg)) {
		response.sendRedirect(URL_SELECT_ASSIGNMENT);
		return;
	}
	
	SubmitAssignment sa = null;
	//get assignment, process upload file
	try {

		//Process upload file
		// Create a factory for disk-based file items
	    FileItemFactory factory = new DiskFileItemFactory();
	 
	    // Create a new file upload handler
	    ServletFileUpload upload = new ServletFileUpload(factory);
				
		//upload MaximumSize
		upload.setFileSizeMax(MAX_FILE_SIZE); //Maximum File size 4MB 
		upload.setSizeMax(MAX_REQUEST_SIZE); //Maximum Request size 4.001MB (need include the size of request text)
		
		//find the upload file & start upload process (only process one upload file)
		List listItem = upload.parseRequest(request);
		Iterator liI = listItem.iterator();
		FileItem uploadItem = null;
		while(liI.hasNext()) {
			FileItem aItem = (FileItem)liI.next();
			if(!aItem.isFormField()) {
				//found the item of upload
				uploadItem = aItem;
				break;
			}
		}
		
		if(uploadItem == null)
			throw new Exception("Can't find upload file");
		
		//get the info of upload fime
		filename = uploadItem.getName();
		fileSize = uploadItem.getSize(); 
		
		//start upload file process
		SubmitAssignmentControl saCon = new SubmitAssignmentControl();
		saCon.uploadFile(uploadItem, ac.getId(), loginUser.getId());
		sa = saCon.getSubmited(ac.getId(), loginUser.getId());
		saCon.closeDBConnection();
		
	} catch (Exception err) {
		errMsg = err.toString();
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Submission Confirm</title>
<style type="text/css">
<!--
.style1 {
	font-size: large;
	font-weight: bold;
}
-->
</style>
</head>

<body>
	<%if(isEmptyString(errMsg)) {%>
	<table width="500" height="185" border="1" bgcolor="#00FF99">
		<tr>
			<td colspan="2"><div align="center">
					<span class="style1">Confirm Submission </span>
				</div>
			</td>
		</tr>
		<tr>
			<td>Student Name (ID)</td>
			<td><%= loginUser.getName() %> (<%= loginUser.getId() %>)</td>
		</tr>
		<tr>
			<td>Assignment Name</td>
			<td><%= ac.getName() %></td>
		</tr>
		<tr>
			<td>SubmitTime (Deadline)</td>
			<td><%= formatDateTime(sa.getSubmitTime()) %> (<%= formatDateTime(ac.getDeadline()) %>)</td>
		</tr>
		<tr>
			<td>Uploaded File Name</td>
			<td><%= filename %></td>
		</tr>
		<tr>
			<td>File Size</td>
			<td><%= (fileSize < FileUtils.ONE_MB) ? ((double)fileSize / FileUtils.ONE_KB) + " KB" : ((double)fileSize / FileUtils.ONE_MB) + " MB"%></td>
		</tr>
		<tr>
			<td colspan="2"><a href="<%= URL_SELECT_ASSIGNMENT %>"
				target="mainFrame">Return to View Assignment</a>
			</td>
		</tr>
	</table>
	<%} else { %>
	<p><%= errMsg %></p>
	<%} %>
</body>
</html>
